

package cn.microservices.cloud.gateway.entity;

import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class JSONResult implements Serializable {
    private Boolean result;
    private String code;
    private String message;
    private final Map<String, Object> data = new HashMap();

    protected JSONResult() {
    }

    public static JSONResult getInstance() {
        return new JSONResult();
    }

    public static JSONResult getInstance(Boolean result, String code, String message) {
        JSONResult json = new JSONResult();
        json.result = result;
        json.code = code;
        json.message = message;
        return json;
    }


    public static byte[] failed(String message) {
        JSONResult json = new JSONResult();
        json.result = false;
        json.code = "01";
        json.message = message;
        return json.toString().getBytes();
    }

    public static byte[] failed(String code, String message) {
        JSONResult json = new JSONResult();
        json.code = code;
        json.result = false;
        json.message = message;
        return json.toString().getBytes();
    }

    public static byte[] failed() {

        return failed("操作失败");
    }


    public Object get(String key) {
        return this.data.get(key);
    }

    public JSONResult setCode(String code) {
        this.code = code;
        return this;
    }

    public JSONResult setMessage(String message) {
        this.message = message;
        return this;
    }

    public JSONResult setResult(Boolean result) {
        this.result = result;
        return this;
    }

    public Boolean isSuccess() {
        return this.result;
    }

    public String getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public String toString() {
        Map json = new HashMap();
        json.put("result", this.result);
        json.put("code", this.code);
        json.put("message", this.message);
        json.put("data", this.data);
        return JSONObject.toJSONString(json);
    }
}
